<?php
//$proId=$_REQUEST['products'];
require_once 'includes/class.phpmailer.php';
session_start();

$userId 		= $_SESSION['user_id_sess'];	
$wishlist_id		= $_REQUEST['wishproduct'];

$qry=mysql_query("select * from tbl_wishlist where wishlist_id=$wishlist_id order by wishlist_id desc limit 1") or die(mysql_error());
$tbl_whslst = mysql_fetch_array($qry);
$prod_id=$tbl_whslst['item_id'];
if($tbl_whslst['add_type']=="new")
{
	$user_id 	= $tbl_whslst['user_id'];
	$wishlist_type	= $tbl_whslst['wishlist_type'];
	$item_id	= $tbl_whslst['item_id'];
	$product_name	= $tbl_whslst['product_name'];
	$price		= $tbl_whslst['price'];
	$description	= $tbl_whslst['description'];
	$product_image	= $tbl_whslst['product_image'];
	$product_thumb	= $tbl_whslst['product_thumb'];
	$product_order	= $tbl_whslst['product_order'];
	$status		= $tbl_whslst['status'];
	$created_date	= $tbl_whslst['created_date'];
	$wantedFor	= $tbl_whslst['wantedFor'];
	$email = '';
}
else
{
   $existing_product_wishlist=mysql_fetch_array(mysql_query("select * from tbl_product where product_id=$prod_id"));
   $existing_product_wishlist_photo=mysql_fetch_array(mysql_query("select * from tbl_product_images where product_id=$prod_id and product_type='p'"));
   $user_id 	= $tbl_whslst['user_id'];
   $wishlist_type	= $tbl_whslst['wishlist_type'];
   $item_id	= $tbl_whslst['item_id'];
   $product_name	= $tbl_whslst['product_name'];
   $price		= $tbl_whslst['price'];
   $description	= $tbl_whslst['description'];
   $product_image	= $existing_product_wishlist_photo['product_sample_1'];
   $product_thumb	= $existing_product_wishlist_photo['product_thumbnail_1'];
   $product_order	= $tbl_whslst['product_order'];
   $status		= $tbl_whslst['status'];
   $created_date	= $tbl_whslst['created_date'];
   $wantedFor	= $tbl_whslst['wantedFor'];
   $email = '';
}
$e_sql = mysql_query("select email from tbl_users where user_id=$user_id") or die(mysql_error());
$ecnt = mysql_num_rows($e_sql);
if($ecnt > 0)
{
	$e_user = mysql_fetch_array($e_sql);
	$email = $e_user['email'];
}

$ss = mysql_query("select googleMapKey from tbl_settings where setting_id=1") or die(mysql_error());
 $tbl_settings = mysql_fetch_array($ss);
 if($tbl_settings) $googleMapKey = $tbl_settings['googleMapKey'];  else $googleMapKey='';
?>

<script language="javascript" type="text/javascript">
function show_ask()
{
  var ntop = ( $(window).height() - $('#ask_popup').height() ) / 2+$(window).scrollTop();
  var nleft = ( $(window).width() - $('#ask_popup').width() ) / 2+$(window).scrollLeft();

  $('#ask_popup').css({'left': nleft, 'top':ntop});
  $('#ask_popup').fadeIn('slow');
}
</script>

<div style=" display: none; width: 400px; font-weight:bold; padding:10px; background-color:#cccccc; opacity:0.9;" class="user_popup" id="ask_popup">
  <span style="float:left; width:100%; text-align:right; margin-bottom:5px;"><label style="cursor:pointer;" onclick="$('#ask_popup').fadeOut('slow');">x</label></span>
  <span style="float:left; width:100%; margin-bottom:10px;" id="desc_info">Please send us the content of Ask Question.</span>
</div>


<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;oe=utf-8&amp;key=<?php echo $googleMapKey; ?>" type="text/javascript"></script>



<style type="text/css">
	span#add-wishlist { color:#0000CC; padding:5px; font-size:12px; }	
</style>
<?php
if($_SESSION['user_id_sess']!='')
{
	$address_of_seller= getUserAddress($userId);
	?>
	<input type="hidden" id="address_of_seller<?php echo $wishlist_id;?>" value="<?php echo $address_of_seller; ?>" />
	<?php
}
?>
								
<script type="text/javascript">
$(document).ready(function() {

	var geocoder, location1, location2, gDir;
	geocoder = new GClientGeocoder();
	gDir = new GDirections();
	var address_of_seller = $('#address_of_seller<?php echo $wishlist_id;?>').val();
	//alert(address_of_seller);
	geocoder.getLocations(address_of_seller, function (response) {
	if (!response || response.Status.code != 200)
	{
		//alert("Sorry, we were unable to geocode the first address");
	}
	else
	{
		location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
		//alert(response.Placemark[0].Point.coordinates[1]);
		//$('#lat<?php echo $proId;?>').val(response.Placemark[0].Point.coordinates[1]);
		//$('#lon<?php echo $proId;?>').val(response.Placemark[0].Point.coordinates[0]);
	}
	});
});
</script>
<script language="javascript1.2">

function sendMail()
{
	var proId = '<?php echo $wishlist_id?>';
	var url = "<?php echo $base_url;?>report_whishlist.php";
	$.post(url,
	{ 'prodId':proId},
	function(data)
	{
		alert('Item successfully reported');
	},
	'html');
}

function show_myinventory_list()
{
	var user_id = '<?php echo $userId; ?>';
	if(user_id=='')
	{
		alert('Please login to select your inventory');
	}
	else
	{
		var url = "<?php echo $base_url;?>my_inventory.php";
		$.post(url,
		{ 'user_id':user_id},
		function(data)
		{
			var pos = $('#brw').position();
			var ntop = pos.top;
			var nleft = pos.left;
			$('#my_inventory_box').css({'top':ntop, 'left':nleft}).show();
			$('#inv_box').html(data);
		},
		'html');
	}
}
function select_inv()
{
	
	var selected = new Array();
	$('#inv_box input:checked').each(function() {
	    selected.push($(this).val());
	
	});
	//alert(selected);
	$('#product_id').val(selected);
	$('#my_inventory_box').hide();
}
</script>
<div style="width: 220px; display:none;" class="srlink_popup2" id="my_inventory_box">
	<div style="float:left; width:210px;">
		<span style="float:left; width:50%; color:#7A2525"><b>My Inventory</b></span>
		<span style="float:left; width:50%; text-align:right; cursor:pointer;" onclick="javascript:$('#my_inventory_box').hide();">x</span>
	</div>
	<div style="width: 200px;" id="inv_box" class="srlinkpp2_text">
		
	</div>
</div
><table width="100%" border="0">

	<tr>
		<td>
			<div style="width: 950px; float: left; vertical-align: top; margin:15px 0px 5px 0px;">
		
				<div class="mypg_left_out">
					<a href="javascript:history.back();" style="text-decoration: none;"><img src="images/arrow_back.png" style="border:none;margin:0px 5px 0px 0px;" />Back to Search Results</a>
					<br /><br />
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0);" style="text-decoration: none;" onclick="sendMail()">Report Item</a>
				</div>
				
		
				
				<div class="fullbox_out">	
					<strong class="orangehead">Search For Buyer</strong>
					<p style="margin: 5px 0px;" class="grey_txt">
						<i>Take selling into your hands by searching through all wanted items and finding the buyer yourself.</i>
					</p>	
				</div>
				     
			</div>
	   	</td>
	</tr>

	<tr>
		<td>
			
	
			<div class="fulldiv">
				
				
	   
	   
				<?php
				if(!empty($_POST['hidBargain']))
				{
					if($_SESSION['user_id_sess']!="")
					{ 
						$askingAmount 	= $productPrice;
						$offerAmount 	= $_POST['offer_price'];
						$comment 	= $_POST['bargain_comment'];
						$product_id	= $_POST['product_id'];
						
						if($offerAmount=="") {
							echo "<div id='err-msg'>Enter offer amount.</div>"; 
						}					
						else
						{
							if($_POST['hidBargain']=="add")
							{							
								$sqlInsBargain="INSERT INTO tbl_bargain set item_id='$proId',asking_amount='$askingAmount', status='offer'";
								$rstInsBargain=mysql_query($sqlInsBargain);	
								$add_rst=mysql_insert_id();
								if($add_rst)
								{ 
									$emailContent="You are receiving this message because (".$_SESSION['user_name'].") has made an offer<br><p>Offer: $".number_format($offerAmount,2)."<p><p>".$comment."<br />To view the details of the product <a href='".$base_url."index.php?products=".$product_id."'>click here</a></p>";
									
									mysql_query("insert into tbl_messages(senderid,recieverid,productid,subject,message,posteddate,status,type,itemid,bargain_id) values('".$_SESSION['user_id_sess']."','".$_POST['productOOwner']."','".$proId."','Offer Recieved','".addslashes($emailContent)."','".date("Y-m-d")."','1','Bargain','".$itemId."','".$add_rst."')");
									$parentid=mysql_insert_id();									
									mysql_query("insert into tbl_messages(senderid,recieverid,productid,subject,message,posteddate,status,type,itemid,parentid) values('".$_SESSION['user_id_sess']."','".$_POST['recieverid']."','".$proId."','Offer Recieved','".addslashes($emailContent)."','".date("Y-m-d")."','1','Bargain','".$itemId."','".$parentid."')");
									
									$mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
									<tr>
										<td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$emailContent."</td>
									</tr>
									</table>";
									$email = $email.", mauzzam@adroitcoders.com";
									$mail = new PHPMailer(); // defaults to using php "mail()"
									$mail->MsgHTML($mailmsg);
									$mail->From       = "mail@swapen.com";
									$mail->FromName   = "Swapen.com";
									$mail->Subject    = "Offer Recieved";
									$mail->AddAddress($email);
									$mail->Send();
	
								
									echo "<div id='suc-msg'>Bargain placed successfully.</div>";
								}
								else
								{
									echo "<div id='err-msg'>Error on placing your bargain.</div>";
								}						
							}						
						}
					}
					else
					{
						echo "<script type='text/javascript'> window.location='index.php?signin&url=bargain'; </script>";
					}
					
				}	
				?>
	   
	  
				
	   
					<div class="desc_div">
						<div class="descbox">
							<table width='100%' border='0' align='center' cellspacing='0'>
								<tr>
									<td>
										<div class="fulldiv">
										      <div style="width:300px; float:left;">
												<?php if($tbl_whslst['add_type']=="new") { ?><img src="<?php echo $product_image; ?>" /><?php } else { if($product_image!='') { ?><img src="<?php echo $product_image; ?>" /><?php } else { ?><img src="images/no_image.gif" /><?php } } ?>			 
										      </div>
										      <div style="width:150px; float:left;">			 
												<?php if($tbl_whslst['add_type']=="new") { ?><img src="<?php echo $product_thumb; ?>" /><?php } else { if($product_thumb!='') { ?><img src="<?php echo $product_thumb; ?>" /><?php } else { ?><img src="images/no_image.gif" /><?php } } ?>
												<div class="fulldiv">
													<!--<a href="javascript:void(0);" onclick="openWindow('item_viewer.php?prod=<?php //echo $proId; ?>&type=i','580','650');"><?php //if(getImageCount($proId)!=0) echo getImageCount($proId)." images"  ?></a>-->
												</div>
										      </div>
										</div>
									</td>
								</tr>
			
								<tr>
									<td>
										<div class="fulldiv">
															
											<div class="greytab_out" style="float:left; margin:15px 1px -8px 25px;">
												<div class="greytab_left">&nbsp;</div>
												<div class="greytab_repeat" style="padding:5px;">
													<b>MAKE OFFER</b><img src="images/cart_ico.png" style="margin:0px 0px -3px 10px;" width="20px;" />
												</div>
												<div class="greytab_right">&nbsp;</div>
											</div>
											
						
											<div class="ads_out" style="margin:0px 0px 0px 10px;">
												<div class="ads_top"></div>
												<div class="ads_repeat">
														
													<form name="frmProductInfo" id="frmProductInfo" method="post">
													<div style="margin: 5px 0px;" class="fulldiv">
														<div style="float: left; width: 390px; margin: 0pt 10px;">
															<span style="float: left; width: 380px;">
																<span style="float: left; width: 90px; margin-top: 3px;">
																	<strong>Wanted Price</strong>
																</span> 
																<span style="float: left; width: 105px; text-align: center;">
																	<font style="color: #c43131; font-size: 13px; font-weight: bold;">$<?php echo number_format($price,2); ?></font>
																	
																</span>
																<span style="float: left; width: 160px;">
																	<input type="text" name="offer_price" id="offer_price" class="inputbg" style="width:95px;" value="your offer price" onBlur="putIndicator(this,'your offer price')" onFocus="clearIndicator(this,'your offer price')" onClick="clearIndicator(this,'your offer price')"  onkeydown="return onlyint(event)"  />
																	<span style="color: red; display:none;" id="digit_err">Digit only</span>
																</span>
																
															</span>
																												
														</div>
														<script>
															function onlyint(event) {
	
																var charCode = event.keyCode || event.which;
																if(event.shiftKey && charCode == 9)
																{return true}
																else  if (event.shiftKey)
																{	$('#digit_err').show().fadeOut("slow"); return false;	}
																else if  (((event.keyCode == 46) || (event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || (event.keyCode == 8) || (event.keyCode == 9)) )
																{ return true; } else { $('#digit_err').show().fadeOut("slow"); return false; }
															}
														</script>
													</div>
													
													
														
												
													<input type="hidden" name="hidBargain" id="hidBargain">
													<div class="fulldiv" style="margin:5px 0px 5px 0px;">	
														<div style="width:220px; float:left; margin:0px 5px 0px 10px;"><strong>Attach Product from your inventory:</strong></div>
														<div style="width:90px; float:left; margin:0px 10px 0px 0px;">
															<input type="button" value="Browse" id="brw" name="brw" class="refer_btn" onclick="show_myinventory_list()" />								
															<input type="hidden" name="product_id" id="product_id" />
														</div>
													</div>
								
													<div class="fulldiv" style="margin:5px 0px 5px 0px;">	
														<div style="width:100px; float:left; margin:0px 5px 0px 10px;"><strong>Add Comment</strong></div>
														<div style="width:280px; float:left; margin:0px 10px 0px 0px;">
														<textarea name="bargain_comment" id="bargain_comment" cols="6" rows="3" style="width:250px;" class="inputbg"></textarea>								
														</div>
													</div>
													
													<div class="fulldiv" style="margin:5px 0px 5px 0px;">	
														<div style="width:100px; float:left; margin:0px 5px 0px 10px;">&nbsp;</div>
														<div style="width:280px; float:left; margin:0px 10px 0px 0px;">
															<input type="button" value="Make Offer" name="" class="refer_btn" onclick="funBargain();" />
														</div>
													</div>	
												</form>
													
												</div>
												<!--end : ads_repeat-->
												<div class="ads_bot"></div>
											</div>
											<!--end:ads_out-->
										</div>			
									</td>
								</tr>
			
								
								<tr>
									<td>
										<div class="fulldiv" style="margin:5px 0px 5px 0px; height:50px;">
										     <a class="bluelink" href="#" style="text-decoration:none;">Refer a friend to buy this item and make extra commission <img src="images/chat.png" height="40px" width="40px" border="0" style="margin:0px 0px -20px 0px;" /></a>		
										</div>
									</td>
								</tr>
							</table>
						</div>
					</div>
					<!--desc_div-->
	   
	   
					<div class="desc_div" style="margin:0px 0px 0px 5px;">
					
						<div class="descbox">
					
							<table width="100%" border="0" align="center">
						 
							<tr>
								<td>
									<div class="fulldiv">
										<strong class="blue_big"><?php echo $product_name;?></strong>&nbsp;
										
									</div>
			
									<div class="fulldiv">
										<div style="float:left; width:270px;">
											<p class="grey_txt pro_ptag">
												<i style="float:left;">Wanted For - <strong><?php if($wantedFor==0) echo 'local pickup only'; else if($wantedFor==1) echo 'shipping only'; else echo 'local pickup or shipping'; ?></strong></i>
											</p>
											<p class="grey_txt pro_ptag">
												<i style="float:left; margin:0px 5px 0px 0px">
													Product Wanted by:
												</i>
												
												<i style="float:left; color:#000000;">
													<?php echo getUser_first_name($user_id); ?>&nbsp;
												</i>
												
												
											</p>
											
										</div>
										
										<?php/*
										<div style="float:right; width:180px; margin:0px 0px 0px 2px; float:left;">
											<p class="grey_txt pro_ptag">
												<i style="float:right;">
													local pick up - <?php if($localPickup==1) echo "available"; else echo "not available"; ?>
												</i>
											</p>
											<p class="grey_txt pro_ptag" style="text-align:right; border:1px solid #898585; padding:3px 0px;">
												<font>
													feedback
												</font>
												<font style="color:#000000;">
													<?php
													echo getfeedback($product_owner);
													?>
												</font>
											</p>
											<p class="pro_ptag">
												<font style="float:right;">
													<a href="seller_store.php?sid=<?php echo $product_seller;?>" class="bluelink">see seller's store</a>
												</font>
											</p>
										</div>	*/?>
					
					
									</div>
									
									<div class="fulldiv">
									
										<div class="greytab_out" style="float:left; margin:15px 5px -8px 25px;">
											<div class="greytab_left">&nbsp;</div>
											<div class="greytab_repeat">
												Description
											</div>
											<div class="greytab_right">&nbsp;</div>
										</div>
										
										<div class="ads_out" style="margin:0px 0px 0px 10px;">
											<div class="ads_top"></div>
											<div class="ads_repeat" style="padding:0px 15px 0px 15px; width:404px;">
																		
												<?php echo $description;?>					
											</div>
											<div class="ads_bot"></div>
										</div>
					
										
									</div>
					
									<div class="fulldiv" style="margin:10px 0px 0px 0px;">
					
										<div style="float:left; width:285px;">
											<p class="grey_big pro_ptag">	
												Contact
											</p>
											<p class="grey_txt pro_ptag">
												<i style="float:left;">
													Product Wanted by:&nbsp;
												</i>
												<i style="float:left; color:#000000;">
													<?php echo getUser_first_name($user_id); ?>&nbsp;
												</i>
											</p>
											
											</i>
											</p>
										</div>	
											
										<div style="float:right; width:155px; margin:0px 0px 0px 2px; float:left;">
											<p class="pro_ptag"  style="text-align:right;">
												<a href="javascript:void(0)" class="bluelink" onclick="show_ask()" id="ask_questions">Ask Question</a>
												<a class="refer_btn" href="#" style="height: 22px; margin: 10px 0 5px; 
												padding: 7px 5px 7px 5px; text-decoration: none;">Chat</a>
											</p>							
										</div>
									</div>
								</td>
							</tr> 
		             
							</table>
						</div>
						<!--descbox-->
					</div>
					<!--desc_div-->	
	   
					<?php /*
					<div class="desc_div"> 			
						<table width="100%" border="0" align="center">
					       
							<tr>
								
								<td width="53%" valign="top">
									<table  border="0" align="center" width="100%">              
										<tr>
											<td width="93%" height="28" colspan="2" class="uline">
			
												<?php
												
												echo "seller_id".$rowSelItem['seller_id']."<br>";
													echo $userId;
												if($rowSelItem['seller_id'] != $userId)
												{
													
												 ?>
												<table align="center" width="90%" border="0">
													<tr>
														<td height="33" align="center">
															<!--<input type="text" size="7" name="buy_amount">-->
															<input type="button" value="Buy" name="btnBuy" onclick="return addtocart_buy('<?php echo $itemId;?>','<?php echo $productName;?>','<?php echo $productPrice;?>','<?php echo $shipping;?>',1,'<?php echo $sales_tax;?>');">									
														</td>
													</tr>
													<tr>
														<td height="32" align="center"><input type="button" value="Add to Cart" onclick="return addtocart('<?php echo $itemId;?>','<?php echo $productName;?>','<?php echo $productPrice;?>','<?php echo $shipping;?>',1,'<?php echo $sales_tax;?>');"></td>
													</tr>
													<tr>
														<td height="27" align="center"><strong>Bargain</strong></td>
													</tr>
						
													<tr>
														<td align="center">
															<table width="84%"  border="0">
																<tr>
																	<td width="50%" align="center">Asking price </td>
																	<td width="50%" align="center">Your offer </td>
																</tr>
																<tr>
																	<td height="38" align="center"><input type="text" readonly size="7" id="asking_price" name="asking_price" value="<?php echo $bargainAmount; ?>"></td>
																	<td align="center"><input type="text" size="7"  name="offer_price" id="offer_price"></td>
																</tr>
																<tr>
																	<td colspan="2" align="center">Include Comment</td>
																</tr>
																<tr>
																	<td colspan="2" align="center">
																		<textarea name="bargain_comment" id="bargain_comment" style="height:150px; width:250px; "></textarea>
																		<input type="hidden" name="recieverid" value="<?php echo $productOwner;?>"/>
																		<input type="hidden" name="productOOwner" value="<?php echo $productOOwner;?>"/>
																		<input type="hidden" name="pname" value="<?php echo $productName;?>"/>
																	</td>
																</tr>
																<tr>
																	<td colspan="2" align="center"><input type="button" <?php if($userId==$productOwner) { echo " disabled "; } ?> name="btnCommit" value="Commit" onClick="funBargain();"></td>
																</tr>
															</table>
														</td>
													</tr>
												</table>
											<?php 	} ?>
											</td>
										</tr>
									</table>
								</td>
							</tr>          
						</table>
					</div>
					      */
					?>
					
				
				<br /> 
			</div>
			<!--end:full_div-->
			
			<?php
			/*
			<div class="desc_div"> 
				<table width="100%" align="center" border="0">
					<tr>
						<td><strong>Questions and Offers</strong></td>
					</tr>
					<tr>
						<td>
							<?php 
							if(!empty($_POST['hidSellerReply']))
							{ 
								$parentBargain = $_POST['hidSellerReply'];
								if($_POST['hidType']=="add") {								
									$key = $_POST['hidReplyFor'];								
									$askAmt = $_POST['ask_amount'];
									$buyAmt = $_POST['buyer_amount'];
									$selCmt = $_POST['seller_comment'];
									if($askAmt[$key]=="") {
										echo "<div id='err-msg'>Enter asking amount.</div>";
									}
									
									else {
										$sqlAddReply="INSERT INTO tbl_bargain set product_id='$proId',
											asking_amount='$askAmt[$key]', 
											user_id='$userId', 
											parent_id='$parentBargain', 
											user_type='1', 
											offer_amount='$buyAmt[$key]', 
											bargain_type=1,
											comment='$selCmt[$key]', 
											status='reply'";
										$rstAddReply=mysql_query($sqlAddReply);		
										$addReply=mysql_insert_id();								
										if($addReply) { echo "<div id='suc-msg'>Your reply posted successfully.</div>"; }
										else { echo "<div id='err-msg'>Error on placing your reply.</div>"; }		
									}
								}
								elseif($_POST['hidType']=="delete") {
									$sqlDelReply="DELETE FROM tbl_bargain where bargain_id='".$_POST['hidSellerReply']."'";
									$rstDelReply=mysql_query($sqlDelReply);									
									if($rstDelReply) { echo "<div id='suc-msg'>Your reply deleted.</div>"; }
									else { echo "<div id='err-msg'>Error on deleting your reply.</div>"; }
								}	 
							}
							?>
							<form name="frmProductOffer" action="" method="post" id="frmProductOffer">
								<input type="hidden" name="hidSellerReply" id="hidSellerReply">
								<input type="hidden" name="hidReplyFor" id="hidReplyFor">
								<input type="hidden" name="hidType" id="hidType">
								<?php
								include_once("product_offer.php");
								?>
							</form>
						</td>
					</tr>
				</table>
			</div>
			*/
			?>
		</td>
	</tr>
</table>
>
